//
//  AddressController.m
//  ai2ban
//
//  Created by Heartbeats on 2018/1/18.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "AddressController.h"
#import "AdressTabCell.h"
#import "AddAdressController.h"

@interface AddressController ()

@end

@implementation AddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的地址";
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"AdressTabCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AdressTabCell"];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadAddres];
}

- (IBAction)setAddressDefaul:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        AppManager.user.defaultAdress = AppManager.user.addresses[indexPath.row];
        if (self.selectAddress) {
            self.selectAddress(AppManager.user.addresses[indexPath.row]);
        }
        [self.navigationController popViewControllerAnimated:true];
    }
}

- (IBAction)editAddress:(UIButton *)sender {
    
}

- (void) loadAddres {
    [ActivityIndicatorView show];
    [[NetworkManager shareNetwork] requestWithURLString:[NSString stringWithFormat:@"app/system/address/list/%@", [AppManager userId]] parameters:nil method:@"" callBack:^(id responseObject) {
        [ActivityIndicatorView dismiss];
        if ([responseObject[@"state"] intValue]) {
            AppManager.user.addresses = [AdressModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.tableView reloadData];
        }
    }];
}

- (IBAction)addAddressAction:(UIButton *)sender {
    AddAdressController *addAdressControl = [[AddAdressController alloc] initWithNibName:@"AddAdressController" bundle:nil];
    [self.navigationController pushViewController:addAdressControl animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return AppManager.user.addresses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AdressTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AdressTabCell" forIndexPath:indexPath];
    cell.adressModel = AppManager.user.addresses[indexPath.item];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    AppManager.user.defaultAdress = AppManager.user.addresses[indexPath.item];
//    [self.navigationController popViewControllerAnimated:true];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[NSBundle mainBundle] loadNibNamed:@"AddressTabHeadView" owner:nil options:nil].firstObject;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
